<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客编辑页</title>

    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/edit.css">
    <link rel="stylesheet" href="blog-editormd/css/editormd.css" />

</head>

<body>
    <div class="nav">
        <img src="" class = "MB_name" alt="">
        <span class="blog-title">我的博客系统</span>
        <div class="space"></div>
        <a class="nav-span" href="blog_list.html">主页</a>
        <a class="nav-span" href="blog_edit.html">写博客</a>
        <a class="nav-span" href="blog_login.html" onclick="logout()">注销</a>
    </div>
    <div class="content-edit">
        <div class="push">
<!--            储存blogId 直接从url中获取比较麻烦-->
            <input type="hidden" id="blogId" >
            <input type="text" name="" id="title">
            <input type="button" value="更新文章" id="submit" onclick="submit()">
        </div>
        <!-- markdown 插件 html代码 -->
        <div id="editor">
            <textarea style="display:none;" id="content" >##在这里写下一篇博客111</textarea>
        </div>
    </div>

    <script src="js/jquery.min.js"></script>
    <script src="blog-editormd/editormd.min.js"></script>
    <script src="js/common.js"></script>
    <script src="js/setting.js"></script>
    <script type="text/javascript">


        $(function () {
            var editor = editormd("editor", {
                width  : "100%",
                height : "550px",
                path: "blog-editormd/lib/"
            });
        });
        
        function submit() {
            $.ajax({
                type: "post",
                url: "blog/updateBlog",
                contentType: "application/json",
                headers: {
                    token: localStorage.getItem("token")
                },
                data: JSON.stringify({
                    id: $("#blogId").val(),
                    userId: localStorage.getItem("userId"),
                    title: $("#title").val(),
                    content: $("#content").val()
                }),
                success: function (result) {
                    console.log("updateInfo: "+result.success);
                    location.href = "blog_list.html";
                },

                error: function (xhr) {
                    //获取状态码
                    if (xhr.status == 406) {
                        alert("未登录");
                        location.href = "blog_login.html";
                        return;
                    }
                }
            });
        }
        function getBlogInfo() {
            $.ajax({
                type: "post",
                url: "blog/getBlogInfoDetail" + location.search,
                contentType: "application/json",
                headers: {
                    token: localStorage.getItem("token")
                },
                //拼接博客信息
                success: function (result) {
                    if (result == null || !result.success) {
                        alert("man what can say response nothing");
                        location.href = "blog_list.html";
                    }

                    var re = result.data;
                    console.log("blog详细信息: " + re);
                    //内容展示 发现使用text这样不能赋值

                    $("#title").val(re.title);
                    $("#blogId").val(re.id);

                    editormd("editor", {
                        width: "100%",
                        height: "550px",
                        path: "blog-editormd/lib/",
                        onload: function () {
                            this.watch();
                            this.setMarkdown(re.content);
                        }
                    });
                },
                error: function (xhr) {
                    //获取状态码
                    if (xhr.status == 406) {
                        alert("未登录");
                        location.href = "blog_login.html";
                        return;
                    }
                }
            });
        }
        getBlogInfo();
    </script>
</body>

</html>